<?

class DB
{
    private $id; # id konektoru k databázi
    var $login = array(server => "localhost",
                       name => "root",
                       password => "",
                       database => "");
    var $lastSQL; # poslední provedený dotaz
    var $countSQL; # počet dotazů
    
    # Konstruktor, přihlášení k serveru a databázi.
    public function Connect ($database = NULL, $server = NULL, $name = NULL, $password = NULL)
    {
        if (isset($database)) $this->login[database] = $database;
        if (isset($server)) $this->login[server] = $server;
        if (isset($name)) $this->login[name] = $name;
        if (isset($password)) $this->login[password] = $password;

        if ($this->id = mysql_connect($this->login[server], $this->login[name], $this->login[password]))
        {
            mysql_select_db ($this->login[database], $this->id);
            mysql_query("SET CHARACTER SET utf8");
            return true;
        }
        else return false;
    }
    
    
    # Vrací jednoduše výsledek SQL dotazu
    public function retQuery ($query)
    {
        $this->countSQL++;
        $this->lastSQL = $query;
        if ($sql = mysql_query($query, $this->id))
        {
            while($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $return[] = $row;
            return $return;
        }
        else
        {
            return mysql_error();
        }
    }
    
    
    public function exeQuery ($query)
    {
        $this->countSQL++;
        $this->lastSQL = $query;
        if (mysql_query($query, $this->id))
        {
            return true;
        }
        else
        {
            return mysql_error();
        }
    }
    
    public function exeUpdate ($table, $where, $data)
    {
        $query = "UPDATE `{$table}` SET ";
        foreach ($data as $key => $val) $array[] = "`{$key}` = '{$val}'";
        $query .= implode(", ", $array);
        $query .= " WHERE {$where}";
        return $this->exeQuery($query);
    }
    
    public function exeInsert ($table, $data) 
    {
        $query = "INSERT INTO `{$table}` SET ";
        foreach ($data as $key => $val) $array[] = "`{$key}` = '{$val}'";
        $query .= implode(", ", $array);
        return $this->exeQuery($query);
    }
    
}

?>
